package com.ai.dmc.utils.export.excelhelper;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class TExcelExporter<T> implements ITExcelExporter<T>{
	private static final String DEFAULTSHEETNAME = "sheet-1";
	private final String fileNotFoundMsg="The file to download not found";
	private final String downloadMsg="Download failed";
	
	private static final Logger logger = Logger.getLogger(TExcelExporter.class);

	private TExcelFactory<T> tExcelFactory;
	
	private DownloadFactory downloadFactory;
	
	public TExcelFactory<T> getTExcelFactory() {
		return tExcelFactory;
	}

	public void setTExcelFactory(TExcelFactory<T> excelFactory) {
		tExcelFactory = excelFactory;
	}

	public DownloadFactory getDownloadFactory() {
		return downloadFactory;
	}

	public void setDownloadFactory(DownloadFactory downloadFactory) {
		this.downloadFactory = downloadFactory;
	}

	/**
	 * 以下载的方式导出单行表头的Excel
	 */
	public void exportExcelByObj(String sheetName, String[] headers,
			Collection<T> dataset, String[] methodNames, String pattern, HttpServletResponse response){
		try {
			String fileName = this.exportToFileByObj(null, null, sheetName, headers, dataset, methodNames, pattern);
			downloadFactory.downloadfile(response, fileName);
		} catch (FileNotFoundException fnfe) {
			logger.error(fileNotFoundMsg);
			logger.debug("ExcelExporter.exportExcelByL():",fnfe);
		} catch (IOException ioe) {
			logger.error(downloadMsg);
			logger.debug("ExcelExporter.exportExcelByL():",ioe);
		}
	}
	
	/**
	 * 将生成的Excel表格文件以指定的方式命名，并保存到指定目录下
	 * @param dir: 临时文件的存放位置，如果为null将放入用户目录下的TEMPEXCELFILE中
	 * @param fileName: Excel文件名称，如果为null或空，则采用sheetName，两者都为空时，则命名为DEFAULTSHEETNAME
	 * @param sheetName：标签页名称，如果为空，则采用fileName
	 * @param headers: 表头
	 * @param dataset：以对象的方式组织的数据
	 * @param methodNames：获取对象属性的方法,如果为null，那么将按照bean中属性的存放顺序来获取
	 * @param pattern：时间的格式，如果为null，则采用默认方式“yyyy-MM-dd”
	 * @return：返回保存文件的路径
	 * @throws IOException
	 */
	public String exportToFileByObj(String dir,String fileName,String sheetName, String[] headers,
			Collection<T> dataset, String[] methodNames, String pattern) throws IOException{
		String dirPath = "";
		if(dir == null || dir.equals("")){
			dirPath = System.getProperty("user.home") + "\\TEMPEXCELFILE";
		}else {
			dirPath = dir;
		}
		if((fileName == null) || (fileName.equals(""))){
			fileName = ((sheetName == null) || (sheetName.equals(""))) ? DEFAULTSHEETNAME+".xls" : sheetName + ".xls";
		}
		if((sheetName == null) || (sheetName.equals(""))){
			sheetName = fileName.substring(0, fileName.lastIndexOf("."));
		}
		if(tExcelFactory == null){
			tExcelFactory = new TExcelFactory<T>();
		}
		HSSFWorkbook workbook = tExcelFactory.createWorkbookByObj(sheetName, headers, dataset, methodNames, pattern);
		POIExcelHelper.save(workbook, dirPath, fileName);
		return dirPath + fileName;
	}
	
	/**
	 * 将生成的Excel表格文件以指定的方式命名，并保存到指定目录下
	 * @param dir: 临时文件的存放位置，如果为null将放入用户目录下的TEMPEXCELFILE中
	 * @param fileName: Excel文件名称，如果为null或空，则采用sheetName，两者都为空时，则命名为DEFAULTSHEETNAME
	 * @param sheetName：标签页名称，如果为空，则采用fileName
	 * @param headerList: 多行表头，以cellbean的形式存在
	 * @param dataset：以对象的方式组织的数据
	 * @param methodNames：获取对象属性的方法,如果为null，那么将按照bean中属性的存放顺序来获取
	 * @param pattern：时间的格式，如果为null，则采用默认方式“yyyy-MM-dd”
	 * @return：返回保存文件的路径
	 * @throws IOException
	 */
	public String exportToFileByObj(String dir, String fileName, String sheetName, List<CellBean> headerList,
			Collection<T> dataset, String[] methodNames, String pattern) throws IOException {
		String dirPath = "";
		if(dir == null || dir.equals("")){
			dirPath = System.getProperty("user.home") + "\\TEMPEXCELFILE";
		}else {
			dirPath = dir;
		}
		if((fileName == null) || (fileName.equals(""))){
			fileName = ((sheetName == null) || (sheetName.equals(""))) ? DEFAULTSHEETNAME+".xls" : sheetName + ".xls";
		}
		if((sheetName == null) || (sheetName.equals(""))){
			sheetName = fileName.substring(0, fileName.lastIndexOf("."));
		}
		
		if(tExcelFactory == null){
			tExcelFactory = new TExcelFactory<T>();
		}
		HSSFWorkbook workbook = tExcelFactory.createWorkbookByObj(sheetName, headerList, dataset, methodNames, pattern);
		POIExcelHelper.save(workbook, dirPath, fileName);
		return dirPath + fileName;
	}

	/**
	 * 以导出的方式导出多行表头的Excel
	 */
	public void exportExcelByObj(String sheetName, List<CellBean> headerList,
			Collection<T> dataset, String[] methodNames, String pattern,
			HttpServletResponse response) {
		try {
			String fileName = this.exportToFileByObj(null, null, sheetName, headerList, dataset, methodNames, pattern);
			downloadFactory.downloadfile(response, fileName);
		} catch (FileNotFoundException fnfe) {
			logger.error(fileNotFoundMsg);
			logger.debug("ExcelExporter.exportExcelByL():",fnfe);
		} catch (IOException ioe) {
			logger.error(downloadMsg);
			logger.debug("ExcelExporter.exportExcelByL():",ioe);
		}
		
	}
}
